﻿using System;
using System.Collections.Generic;
using BYFW.BOL;
using System.Data;
using BYFW.DAL;

namespace LDSP.BRL
{
    public class SystemConfig : BaseConfigEntity
    {
        protected SystemConfig()
        {
            m_tableName = "t_systemConfig";
            m_hasCode = true;
            m_hasComment = true;
            m_hasName = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected static SystemConfig s_cfg = null;
        protected static SystemConfig CFG
        {
            get
            {
                if (s_cfg == null)
                {
                    s_cfg = new SystemConfig();
                }
                return s_cfg;
            }
        }

        /// <summary>
        /// 获取指定的Config，若其不存在，则使用默认值创建一个
        /// </summary>
        /// <param name="code">指定的Config代号</param>
        /// <param name="dft">当config不存在时，使用此默认值创建一个</param>
        /// <returns></returns>
        protected static SystemConfig GetConfigByCode(string code, object dft)
        {
            DataRow dr = CFG.GetRecordByCode(code);
            SystemConfig tmp = new SystemConfig();
            if (dr != null)
            {
                tmp.GetProperties(dr);
            }
            else
            {
                tmp.Code = code;
                if (dft != null)
                {
                    tmp.Comment = dft.ToString();
                    tmp.Name = dft.ToString();
                }
                tmp.Add();
            }
            return tmp;
        }

        /// <summary>
        /// 移除指定的Config
        /// </summary>
        /// <param name="code"></param>
        public static void RemoveConfig(string code)
        {
            DataRow dr = CFG.GetRecordByCode(code);
            SystemConfig tmp = new SystemConfig();
            if (dr != null)
            {
                tmp.GetProperties(dr);
                tmp.Delete();
            }
        }
        /// <summary>
        /// 给指定的config设置默认值
        /// </summary>
        /// <param name="code"></param>
        /// <param name="dft"></param>
        public static void SetDefault(string code, object dft)
        {
            DataRow dr = CFG.GetRecordByCode(code);
            SystemConfig tmp = new SystemConfig();
            if (dr != null)
            {
                tmp.GetProperties(dr);
                tmp.Comment = dft != null ? dft.ToString() : "";
                tmp.Update();
            }
        }

        #region System Config

        /// <summary>
        /// 最大下载次数
        /// </summary>
        public static string MaxDownloadTimes
        {
            get
            {
                return GetConfigByCode("MaxDownloadTimes", "20").Name;
            }
            set
            {
                if (MaxDownloadTimes != value)
                {
                    SystemConfig tmp = GetConfigByCode("MaxDownloadTimes", null);
                    tmp.Name = value;
                    tmp.Update();
                }
            }
        }
        /// <summary>
        /// 任务计划提醒时间段
        /// </summary>
        public static string ExpireAlertDays
        {
            get
            {
                return GetConfigByCode("ExpireAlertDays", "20").Name;
            }
            set
            {
                if (ExpireAlertDays != value)
                {
                    SystemConfig tmp = GetConfigByCode("ExpireAlertDays", null);
                    tmp.Name = value;
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 失效提醒时间段
        /// </summary>
        public static string InvalidAlertDays
        {
            get
            {
                return GetConfigByCode("InvalidAlertDays", "20").Name;
            }
            set
            {
                if (InvalidAlertDays != value)
                {
                    SystemConfig tmp = GetConfigByCode("InvalidAlertDays", null);
                    tmp.Name = value;
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 新增法规提醒时间段
        /// </summary>
        public static string AddNewAlertDays
        {
            get
            {
                return GetConfigByCode("AddNewAlertDays", "20").Name;
            }
            set
            {
                if (AddNewAlertDays != value)
                {
                    SystemConfig tmp = GetConfigByCode("AddNewAlertDays", null);
                    tmp.Name = value;
                    tmp.Update();
                }
            }
        }

        #endregion
    }

}